<?php
class SaveVip extends Action {
	public function execute($params) {
		$me = check_login ( ADMIN_LOGIN_PAGE );
		$user = rqsts ( array ('uid' => 0, 'account', 'gname', 'email', 'status', 'passwd', 'passwd1', 'sns_from', 'old_account', 'old_email' ), true );
		
		$userModel = new VipUserModel ();
		if (empty ( $user ['uid'] )) { // 新增
			unset ( $user ['uid'] );
			$user ['reg_ip'] = $_SERVER ['REMOTE_ADDR'];
			$user ['last_ip'] = $_SERVER ['REMOTE_ADDR'];
		} else {
			if (empty ( $user ['passwd'] )) { // 修改时如果没设置密码，则不修改密码
				unset ( $user ['passwd'], $user ['passwd1'] );
			}
		}
		//验证用户数据的合法性
		$valid = $this->validate ( 'vip', $user, $userModel );
		if ($valid === true) { //用户数据合法
			unset ( $user ['passwd1'] );
			if (! empty ( $user ['passwd'] )) {
				$user ['password'] = $user ['passwd'];
				$user ['passwd'] = md5 ( $user ['passwd'] );
			}
			$userModel->begin ();
			$rst = $userModel->save ( $user );
			if (! empty ( $rst )) {
				$user ['uid'] = $rst ['uid'];
				$rst = apply_filter ( 'on_save_vip_user', $user ); //保存（新增，修改)
				if ($rst) {
					$userModel->commit ();
					if (! rqset ( 'goon' )) {
						sess_add ( 'page_tip_info', '<strong>恭喜,</strong>会员保存成功.' );
						Response::redirect ( './?Ctlr=EditVip&suc=1&uid=' . $rst ['uid'] );
					} else {
						Response::redirect ( './?Ctlr=AddVip' );
					}
				}
			}
			$userModel->rollback ();
			sess_add ( 'vip_form_info', $user );
			sess_add ( 'page_tip_info', '<strong>出错啦!</strong>' . $userModel->last_error_msg () );
			Response::redirect ( $this->referer );
		}
		sess_add ( 'vip_form_error', $valid );
		sess_add ( 'vip_form_info', $user );
		Response::redirect ( $this->referer );
	}
}